package com.bf.electroplating.controller.front;

import com.bf.electroplating.pojo.dto.frontPage.WarningDataDTO;
import com.bf.electroplating.pojo.vo.ResultVO;
import com.bf.electroplating.pojo.vo.warning.WarningDataVO;
import com.bf.electroplating.pojo.vo.warning.WarningQueryDTO;
import com.bf.electroplating.service.IFrontWarningService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

/**
 * 前端预警信息控制器
 * 提供预警信息相关的数据接口
 */
@RestController
@RequestMapping("/api/front/warning")
@Tag(name = "前端预警信息", description = "前端预警信息相关接口")
public class FrontWarningController {

    @Autowired
    private IFrontWarningService frontWarningService;

    /**
     * 获取预警信息列表
     * 提供预警信息的分页列表和统计数据
     */
    @PostMapping("/list")
    @Operation(summary = "获取预警信息列表", description = "获取预警信息的分页列表和统计数据")
    public ResultVO<WarningDataVO> getWarningList(@RequestBody WarningQueryDTO queryDTO) {
        WarningDataVO data = frontWarningService.getWarningData(queryDTO);
        return ResultVO.ok(data);
    }

    /**
     * 获取所有预警信息列表（不分页）
     * 默认获取未处理的预警信息
     */
    @PostMapping("/all")
    @Operation(summary = "获取所有预警信息列表", description = "获取所有预警信息列表，默认显示未处理的预警")
    public ResultVO<WarningDataVO> getAllWarnings(@RequestBody  WarningDataDTO warningDataDTO
           ) {
        WarningQueryDTO queryDTO = new WarningQueryDTO();
        queryDTO.setPageNum(1);
        queryDTO.setPageSize(1000); // 设置较大的页面大小来获取所有数据
        queryDTO.setHandleStatus(warningDataDTO.getHandleStatus());
        queryDTO.setModule(warningDataDTO.getModule());
        queryDTO.setWarningType(warningDataDTO.getWarningType());
        if (!StringUtils.isEmpty(warningDataDTO.getTime())) {
            queryDTO.setStartTime(LocalDate.parse(warningDataDTO.getTime()).atTime(0,0,0));
            queryDTO.setEndTime(LocalDate.parse(warningDataDTO.getTime()).atTime(23,59,59));
        }
        queryDTO.setPageNum(warningDataDTO.getPageNum());
        queryDTO.setPageSize(warningDataDTO.getPageSize());
        queryDTO.setCompanyId(warningDataDTO.getCompanyId());
        WarningDataVO data = frontWarningService.getWarningData(queryDTO);
        return ResultVO.ok(data);
    }

    /**
     * 根据模块获取预警信息
     * 支持按模块筛选预警信息
     */
    @GetMapping("/by-module")
    @Operation(summary = "根据模块获取预警信息", description = "根据指定模块获取预警信息列表")
    public ResultVO<WarningDataVO> getWarningsByModule(
            @Parameter(description = "预警模块", required = true)
            @RequestParam String module,
            @Parameter(description = "页码", required = false)
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页大小", required = false)
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        
        WarningQueryDTO queryDTO = new WarningQueryDTO();
        queryDTO.setPageNum(pageNum);
        queryDTO.setPageSize(pageSize);
        queryDTO.setModule(module);
        
        WarningDataVO data = frontWarningService.getWarningData(queryDTO);
        return ResultVO.ok(data);
    }

    /**
     * 处理预警信息
     * 将预警状态标记为已处理
     */
    @PostMapping("/handle")
    @Operation(summary = "处理预警信息", description = "将指定的预警信息标记为已处理")
    public ResultVO<String> handleWarning(
            @Parameter(description = "预警ID", required = true)
            @RequestParam String warningId,
            @Parameter(description = "处理备注")
            @RequestParam(value = "handleRemark", required = false) String handleRemark) {
        
        boolean success = frontWarningService.handleWarning(warningId, handleRemark);
        
        if (success) {
            return ResultVO.ok("处理成功");
        } else {
            return ResultVO.fail("处理失败");
        }
    }

    /**
     * 批量处理预警信息
     * 批量将多个预警标记为已处理
     */
    @PostMapping("/batch-handle")
    @Operation(summary = "批量处理预警信息", description = "批量将多个预警信息标记为已处理")
    public ResultVO<String> batchHandleWarnings(
            @Parameter(description = "预警ID列表", required = true)
            @RequestBody String[] warningIds,
            @Parameter(description = "处理备注")
            @RequestParam(value = "handleRemark", required = false) String handleRemark) {
        
        int successCount = 0;
        for (String warningId : warningIds) {
            if (frontWarningService.handleWarning(warningId, handleRemark)) {
                successCount++;
            }
        }
        
        return ResultVO.ok(String.format("批量处理完成，成功处理 %d/%d 条预警", successCount, warningIds.length));
    }
}
